package com.longxi.tianjintianbao.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.utils.MyUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by cxw on & 2018/11/8
 */
public class UpdateComplaintDialog extends Dialog {

    public UpdateComplaintDialog(@NonNull Context context) {
        super(context);
    }

    public UpdateComplaintDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected UpdateComplaintDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder{
        private Context context;
        private OnClickListener listener;
        private String title;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setProperty(OnClickListener listener) {
            this.listener = listener;
            return this;
        }

        private TextView tv_title;
        private RadioGroup rg_reportstate;
        private RadioGroup rg_reportnature;
        private RadioGroup rg_reporttype;
        private RadioGroup rg_reportcategory;
        private TextView tv_cancle;
        private TextView tv_commit;
        private String reportstate = "有效投诉";// 投报状态
        private String reportnature = "一般";// 投报性质
        private String reporttype = "综合服务";// 投保类型
        private String reportcategory = "合同进度";// 投保类别


        private UpdateComplaintDialog dialog;

        public UpdateComplaintDialog create() {
            dialog = new UpdateComplaintDialog(context);
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_update_complaint, null);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句，取消原来的标题栏，没加这句之前，发现在三星的手机上会有一条蓝色的线
            dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            tv_title = view.findViewById(R.id.tv_title);
            rg_reportstate = view.findViewById(R.id.rg_reportstate);
            rg_reportnature = view.findViewById(R.id.rg_reportnature);
            rg_reporttype = view.findViewById(R.id.rg_reporttype);
            rg_reportcategory = view.findViewById(R.id.rg_reportcategory);
            tv_cancle = view.findViewById(R.id.tv_cancle);
            tv_commit = view.findViewById(R.id.tv_commit);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            tv_cancle.setOnClickListener(v -> dialog.dismiss());
            tv_commit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClick(reportstate, reportnature, reporttype, reportcategory);
                }
            });

            rg_reportstate.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(R.id.rBtnYXTS == checkedId){
                        reportstate = "有效投诉";
                    }else{
                        reportstate = "无效投诉";
                    }

                }
            });

            rg_reportnature.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(R.id.rBtnYB == checkedId){
                        reportnature = "一般";
                    }else{
                        reportnature = "严重";
                    }

                }
            });

            rg_reporttype.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(R.id.rBtnZHFW == checkedId){
                        reporttype = "综合服务";
                    }else{
                        reporttype = "物业服务";
                    }

                }
            });

            rg_reportcategory.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(R.id.rBtnHTJD == checkedId){
                        reportcategory = "合同进度";
                    }else if (R.id.rBtnYJTK == checkedId) {
                        reportcategory = "押金退款";
                    }else if (R.id.rBtnFWZL == checkedId) {
                        reportcategory = "房屋质量";
                    }else {
                        reportcategory = "服务态度";
                    }
                }
            });

            return dialog;
        }

        public void setRadioGroup(String reportstate, String reportnature, String reporttype, String reportcategory){
            this.reportstate = reportstate;
            this.reportnature = reportnature;
            this.reporttype = reporttype;
            this.reportcategory = reportcategory;
            if("无效投诉".equals(reportstate)){
                rg_reportstate.check(R.id.rBtnWXTS);
            }else{
                rg_reportstate.check(R.id.rBtnYXTS);
            }

            if("严重".equals(reportnature)){
                rg_reportnature.check(R.id.rBtnYZ);
            }else{
                rg_reportnature.check(R.id.rBtnYB);
            }

            if("物业服务".equals(reporttype)){
                rg_reporttype.check(R.id.rBtnWYFW);
            }else{
                rg_reporttype.check(R.id.rBtnZHFW);
            }

            if("服务态度".equals(reportcategory)){
                rg_reportcategory.check(R.id.rBtnFWTD);
            }else if("房屋质量".equals(reportcategory)){
                rg_reportcategory.check(R.id.rBtnFWZL);
            }else if("押金退款".equals(reportcategory)){
                rg_reportcategory.check(R.id.rBtnYJTK);
            }else{
                rg_reportcategory.check(R.id.rBtnHTJD);
            }
        }

        public void dismiss(){
            if(dialog != null){
                dialog.dismiss();
            }
        }

        public void show(){
            if(dialog != null){
                dialog.show();
            }
        }

    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(layoutParams);
    }

    public interface OnClickListener{
        void onClick(String reportstate, String reportnature, String reporttype, String reportcategory);
    }

}
